<template>
  <div class="loginFrame">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h2>订餐出餐系统</h2>
          <p>管理端登录</p>
        </div>
      </template>

      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        @keyup.enter="handleLogin"
      >
        <div>
          <label class="label">账号</label>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入账号"
              prefix-icon="User"
              clearable
            />
          </el-form-item>
        </div>

        <div>
          <label class="label">密码</label>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
        </div>

        <el-form-item>
          <el-button
            type="primary"
            @click="handleLogin"
            :loading="loading"
            style="width: 100%; margin-top: 20px"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="footer">
        <el-link @click="goToRegister" type="primary" underline="never">没有账号?点击注册</el-link>
        <el-link @click="goToForgetPassword" type="primary" underline="never">忘记密码?</el-link>
      </div>
    </el-card>
  </div>
</template>

<script lang="js" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import router from '@/router'
import axios from 'axios'

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
})

const rememberMe = ref(false)
const loading = ref(false)
const loginFormRef = ref()

// 登录方法
const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      //  模拟登录逻辑
      const {username, password} = loginForm;
      console.log(username,password); // 打印登录信息
      const data = {
        username,
        password
      }
      axios.post('http://localhost:8081/admin/login', data).then(res => { 
        if(res.data && res.data.code === 200){
          ElMessage.success('登录成功')
          loading.value = false
          router.push('/admin/layout')
        }
      })
    } else {
      return false
    }
  })
}

// 去注册
const goToRegister = () => {
  router.push('/admin/register')
}
// 去忘记密码
const goToForgetPassword = () => {
  router.push('/admin/forget')
}
</script>

<style scoped>
.loginFrame {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(120deg, #fdfbfb 0%, #ebedee 100%);
}

.login-card {
  width: 400px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  text-align: center;
}

.card-header h2 {
  margin-bottom: 8px;
  color: #409eff;
}

.card-header p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 10px 0;
}

.label {
    display: block;
    margin-bottom: 7px;
    font-size: 13px;
}
</style>
